import Mock from 'mockjs'
import { param2Obj } from '../src/utils'

import user from './user'
import table from './table'
import appSetting from './appSetting'
import shopping_shop_goods from './shopping/shop/goods'
import shopping_shop_order from './shopping/shop/order'
import shopping_shop_redpack from './shopping/redpack/index'
import general_sms from './general/sms'
import general_push from './general/push'
import general_video from './general/video'
import general_agreement from './general/agreement'
import task_order from './task/order'
import task_workManual from './task/workManual'
import task_taskRule from './task/taskRule'
import elec_grafe from './elec/grafe'
import elec_certificate from './elec/certificate'
import elec_work_demande from './elec/work/demande'
import task_taskModel from './task/taskModel'
import elec_label from './elec/label'
import elec_crafttype from './elec/crafttype'
import task_taskNodeModel from './task/taskNodeModel'
import task_task from './task/task'
import elec_elec_personal from './elec/elec/personal'
import general_guide_image from './general/guide/image'
import general_objAgreement from './general/objAgreement'
import elec_testbatch from './elec//testbatch'
import task_estimate from './task/estimate'
import finance_finance from './finance/finance'
import task_taskAccount from './task/taskAccount'
import general_app from './general/app'
const mocks = [
  ...general_objAgreement,
  ...task_task,
  ...general_guide_image,
  ...elec_elec_personal,
  ...task_taskRule,
  ...task_workManual,
  ...general_agreement,
  ...elec_work_demande,
  ...general_video,
  ...task_order,
  ...general_sms,
  ...general_push,
  ...shopping_shop_goods,
  ...shopping_shop_order,
  ...appSetting,
  ...user,
  ...table,
  ...shopping_shop_redpack,
  ...elec_grafe,
  ...elec_certificate,
  ...task_taskModel,
  ...elec_label,
  ...elec_crafttype,
  ...task_taskNodeModel,
  ...elec_testbatch,
  ...task_estimate,
  ...finance_finance,
  ...task_taskAccount,
  ...general_app
]

// for front mock
// please use it cautiously, it will redefine XMLHttpRequest,
// which will cause many of your third-party libraries to be invalidated(like progress event).
export function mockXHR() {
  // mock patch
  // https://github.com/nuysoft/Mock/issues/300
  Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
  Mock.XHR.prototype.send = function() {
    if (this.custom.xhr) {
      this.custom.xhr.withCredentials = this.withCredentials || false

      if (this.responseType) {
        this.custom.xhr.responseType = this.responseType
      }
    }
    this.proxy_send(...arguments)
  }

  function XHR2ExpressReqWrap(respond) {
    return function(options) {
      let result = null
      if (respond instanceof Function) {
        const { body, type, url } = options
        // https://expressjs.com/en/4x/api.html#req
        result = respond({
          method: type,
          body: JSON.parse(body),
          query: param2Obj(url)
        })
      } else {
        result = respond
      }
      return Mock.mock(result)
    }
  }

  for (const i of mocks) {
    Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
  }
}

// for mock server
const responseFake = (url, type, respond) => {
  return {
    url: new RegExp(`/mock${url}`),
    type: type || 'get',
    response(req, res) {
      res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
    }
  }
}

export default mocks.map(route => {
  return responseFake(route.url, route.type, route.response)
})
